package com.kfm.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kfm.hotel.domain.*;
import com.kfm.hotel.service.SysRoleMenuService;
import com.kfm.hotel.service.SysRoleService;
import com.kfm.hotel.mapper.SysRoleMapper;
import com.kfm.hotel.service.SysUserRoleService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @author 10854
* @description 针对表【sys_role(角色表)】的数据库操作Service实现
* @createDate 2023-06-26 10:28:04
*/
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
    implements SysRoleService{

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Override
    public PageInfo<SysRole> list(SysRole role, Integer page, Integer limit) {
        // 分页
        PageHelper.startPage(page, limit);
        // 查询条件
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();

        // 查询并封装成 PageInfo 对象
        List<SysRole> sysRoles = list(wrapper);
        PageInfo<SysRole> pageInfo = new PageInfo<>(sysRoles);
        return pageInfo;
    }

    @Override
    @Transactional
    public void setRoleMenu(RoleMenuDTO roleMenuDTO) {
        // 把之前所有的角色菜单信息删掉
        UpdateWrapper<SysRoleMenu> deleteWrapper = new UpdateWrapper<>();
        deleteWrapper.eq("role_id", roleMenuDTO.getRoleId());
        sysRoleMenuService.remove(deleteWrapper);

        List<SysRoleMenu> list = new ArrayList<>();
        List<Integer> menus = roleMenuDTO.getMenus();

        for (int i = 0; i < menus.size(); i ++){
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(roleMenuDTO.getRoleId());
            sysRoleMenu.setMenuId(menus.get(i));
            list.add(sysRoleMenu);
        }
        sysRoleMenuService.saveBatch(list);
    }
}




